/**
  ******************************************************************************
  * @file    COMM_DRIVER.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   ACAD Handling
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	COMM_DRIVER_GLOBALS
#define	COMM_DRIVER_GLOBALS
#include <includes.h>




/**
  * @brief  This function handles TIM4 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM4_IRQHandler(void)
{ 
	uint16_t   CaptureValue;
	OS_CPU_SR  cpu_sr;
  
  OS_ENTER_CRITICAL();                         /* Tell uC/OS-II that we are starting an ISR          */
  OSIntEnter();
  OS_EXIT_CRITICAL();
	
	
  /* Timer Overflow */
  if(TIM_GetITStatus(CfgTIM4, TIM_IT_Update) == SET) 
  {
  	/* Clear TIM4 Overflow interrupt pending bit */
    TIM_ClearITPendingBit(CfgTIM4, TIM_IT_Update);
  	
  }
  
  
  /* OC1 Timer Overflow */
  if(TIM_GetITStatus(CfgTIM4, TIM_IT_CC1) == SET) 
  {
  	TIM_ClearITPendingBit(CfgTIM4, TIM_IT_CC1);
   	CaptureValue = TIM_GetCounter(CfgTIM4);
  	TIM_SetCompare1(CfgTIM4, CaptureValue + RS232TIM_Pulse );
    
  	if(RS232RXOverTiCNT)
  	  RS232RXOverTiCNT--;
  	if(RS232RXOverTiCNT==0)
    {
      /* Disable Time Over counter */
      TIM_ITConfig(CfgTIM4,RS232TIM_IT_CCx, DISABLE);
      /* Disable DMA USART RX channel */
      DMA_Cmd(RS232RXDMA_Channel, DISABLE);
      DMA_RS232RXConfig((uint32_t)RS232RXBufferData,CommPTCByteSize_STRPack);
      
  //  USART_ClearITPendingBit(APP_RS232, APP_RS232_RX_ITx);
      USART_ITConfig(APP_RS232, APP_RS232_RX_ITx, ENABLE);
    }
  }

  /* OC3 Timer Overflow */
  if(TIM_GetITStatus(CfgTIM4, TIM_IT_CC3) == SET) 
  {
  	TIM_ClearITPendingBit(CfgTIM4, TIM_IT_CC3);
  	TIM_ITConfig(CfgTIM4,TIM_IT_CC3, DISABLE);
    #if 0
  	/* Reconfig DMA */
  	DMA_Cmd(WIFIRXDMA_Channel, DISABLE);
  	
  	uint8_t os_err;
  	WIFIRXDataNumberBF [WIFIGetDataINTMode]=WIFIRXDataNumV-DMA_GetCurrDataCounter(WIFIRXDMA_Channel);
  	WIFIRXDataReadyFlag[WIFIGetDataINTMode]=DEF_TRUE;
  	OSFlagPost(App_Flags_WIFI,WIFIRXDataAreaOSFlagsList[WIFIGetDataINTMode],OS_FLAG_SET,&os_err);
  	
  	WIFIGetDataINTMode++;
  	if(WIFIGetDataINTMode>=WIFIRXAreaNum)
  		WIFIGetDataINTMode=0;
  	DMA_Init(WIFIRXDMA_Channel,(DMA_InitTypeDef *)WIFIRXxDMA_InitStruAdr[WIFIGetDataINTMode]);
  	
  	/* Continue to RX */
//  USART_ClearITPendingBit(APP_WIFI, APP_WIFI_RX_ITx);
    USART_ITConfig(APP_WIFI, APP_WIFI_RX_ITx, ENABLE);
    #endif
  }
 
  /* OC2 Timer Overflow */
  if(TIM_GetITStatus(TIM4, TIM_IT_CC2) == SET) 
  {
  	TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
  	TIM_ITConfig(TIM4,TIM_IT_CC2, DISABLE);
  	
    USBRXBufferCount=0;
  }
  
  OSIntExit();
}











/************************************/
/*    Initial Comm Timer */
/************************************/
static void TIM_CommTimerControl(FunctionalState TIMCtrSta);
static void RCC_CommTimerCfg  (void);
static void GPIO_CommTimerCfg (void);
static void TIM_CommTimerCfg  (void);

/* Initial Comm Timer Config */
void PORInit_CommTimer  (void)
{
  /* System Clocks Configuration */
  RCC_CommTimerCfg();

  /* Control Timer -------*/
	TIM_CommTimerControl(DISABLE);

  /* Configure the GPIO ports */
  GPIO_CommTimerCfg();
  
  /* Configure the Timer */
  TIM_CommTimerCfg();
  
  /* Control Timer -------*/
	TIM_CommTimerControl(ENABLE);
}



/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
static void RCC_CommTimerCfg(void)
{
  /* TIM clock enable */
  RCC_APB1PeriphClockCmd(CfgTIM4_RCCPeriph, ENABLE);
  
  RCC_APB2PeriphClockCmd(RCCPeriph_USBDetection, ENABLE);
}

/**
  * @brief  Configure the GPIOD Pins.
  * @param  None
  * @retval None
  */
static void GPIO_CommTimerCfg(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
  GPIO_InitStructure.GPIO_Pin   = GPIOxBitx_USBDetection;
  GPIO_InitStructure.GPIO_Speed = GPIOSpeed_USBDetection;
  GPIO_InitStructure.GPIO_Mode  = GPIOModeI_USBDetection;
  GPIO_Init(GPIOxPort_USBDetection, &GPIO_InitStructure);
}


/**
  * @brief  Configure TIM.
  * @param  None
  * @retval None
  */
static void TIM_CommTimerCfg(void)
{
	TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  /* TIM config */
  /* Choose Internal Clock */
  TIM_InternalClockConfig(CfgTIM4);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler         = CfgTIM4_Prescaler        ;
  TIM_TimeBaseStructure.TIM_CounterMode       = CfgTIM4_CounterMode      ;
  TIM_TimeBaseStructure.TIM_Period            = CfgTIM4_Period           ;
  TIM_TimeBaseStructure.TIM_ClockDivision     = CfgTIM4_ClockDivision    ;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = CfgTIM4_RepetitionCounter;
  TIM_TimeBaseInit(CfgTIM4, &TIM_TimeBaseStructure);
  
  /* Output Compare Active Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode       = RS232TIM_OCMode      ;
  TIM_OCInitStructure.TIM_OutputState  = RS232TIM_OutputState ;
  TIM_OCInitStructure.TIM_OutputNState = RS232TIM_OutputNState;
  TIM_OCInitStructure.TIM_Pulse        = RS232TIM_Pulse       ;
  TIM_OCInitStructure.TIM_OCPolarity   = RS232TIM_OCPolarity  ;
  TIM_OCInitStructure.TIM_OCNPolarity  = RS232TIM_OCNPolarity ;
  TIM_OCInitStructure.TIM_OCIdleState  = RS232TIM_OCIdleState ;
  TIM_OCInitStructure.TIM_OCNIdleState = RS232TIM_OCNIdleState;
  TIM_OC1Init(CfgTIM4, &TIM_OCInitStructure);
  /* Enable reload CR1 after EVENT */
  TIM_OC1PreloadConfig(CfgTIM4, TIM_OCPreload_Disable);

  /* Output Compare Active Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OCMode       = WIFITIM_OCMode      ;
  TIM_OCInitStructure.TIM_OutputState  = WIFITIM_OutputState ;
  TIM_OCInitStructure.TIM_OutputNState = WIFITIM_OutputNState;
  TIM_OCInitStructure.TIM_Pulse        = WIFITIM_Pulse       ;
  TIM_OCInitStructure.TIM_OCPolarity   = WIFITIM_OCPolarity  ;
  TIM_OCInitStructure.TIM_OCNPolarity  = WIFITIM_OCNPolarity ;
  TIM_OCInitStructure.TIM_OCIdleState  = WIFITIM_OCIdleState ;
  TIM_OCInitStructure.TIM_OCNIdleState = WIFITIM_OCNIdleState;
  TIM_OC3Init(CfgTIM4, &TIM_OCInitStructure);
  /* Enable reload CR3 after EVENT */
  TIM_OC3PreloadConfig(CfgTIM4, TIM_OCPreload_Disable);
  
  /* Output Compare Active Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OCMode       = USBTIM_OCMode      ;
  TIM_OCInitStructure.TIM_OutputState  = USBTIM_OutputState ;
  TIM_OCInitStructure.TIM_OutputNState = USBTIM_OutputNState;
  TIM_OCInitStructure.TIM_Pulse        = USBTIM_Pulse       ;
  TIM_OCInitStructure.TIM_OCPolarity   = USBTIM_OCPolarity  ;
  TIM_OCInitStructure.TIM_OCNPolarity  = USBTIM_OCNPolarity ;
  TIM_OCInitStructure.TIM_OCIdleState  = USBTIM_OCIdleState ;
  TIM_OCInitStructure.TIM_OCNIdleState = USBTIM_OCNIdleState;
  TIM_OC2Init(CfgTIM4, &TIM_OCInitStructure);
  /* Enable reload CR2 after EVENT */
  TIM_OC2PreloadConfig(CfgTIM4, TIM_OCPreload_Disable);  



}


/**
  * @brief  Control TIM.
  * @param  None
  * @retval None
  */
static void TIM_CommTimerControl(FunctionalState TIMCtrSta)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Disable TIM counter */
  TIM_Cmd(CfgTIM4, DISABLE);
  
  /* TIM Update Interrupt config */
  /* Clear TIM Update Flag */
  TIM_ClearFlag(CfgTIM4,CfgTIM4_FLAGx);
  /* Clear TIM Update Pending Bit */
  TIM_ClearITPendingBit(CfgTIM4,CfgTIM4_ITx);
  
  /* Enable/Disable the TIM Update Interrupt Request */
  TIM_ITConfig(CfgTIM4,RS232TIM_IT_CCx,DISABLE);
  TIM_ITConfig(CfgTIM4,WIFITIM_IT_CCx,DISABLE);
  TIM_ITConfig(CfgTIM4,USBTIM_IT_CCx  ,DISABLE);
  TIM_ITConfig(CfgTIM4,CfgTIM4_ITx ,DISABLE);
  
  /* TIM NVIC config */
  /* Enable the TIMx global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel                   = CfgTIM4_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CfgTIM4_IRQPrePrio;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CfgTIM4_IRQSubPrio;
  NVIC_InitStructure.NVIC_IRQChannelCmd                = TIMCtrSta;
  NVIC_Init(&NVIC_InitStructure);
  
  /* TIM enable/disable counter */
  TIM_Cmd(CfgTIM4, TIMCtrSta);
}



/*$PAGE*/#endif	/*	COMM_DRIVER_GLOBALS	*/
